package com.heu.blood.storge.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heu.blood.common.enums.BloodInformationEnum;
import com.heu.blood.match.dao.BloodInformationDao;
import com.heu.blood.match.entity.BloodInformationEntity;
import com.heu.blood.storge.vo.BloodStorgeWarningVo;
import com.heu.blood.storge.vo.BloodWarningVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heu.blood.common.utils.PageUtils;
import com.heu.blood.common.utils.Query;

import com.heu.blood.storge.dao.BloodStorgeWarningDao;
import com.heu.blood.storge.entity.BloodStorgeWarningEntity;
import com.heu.blood.storge.service.BloodStorgeWarningService;

import static com.heu.blood.common.enums.CommonEnum.IF_SHOW;


@Slf4j
@Service("bloodStorgeWarningService")
public class BloodStorgeWarningServiceImpl extends ServiceImpl<BloodStorgeWarningDao, BloodStorgeWarningEntity> implements BloodStorgeWarningService {
    @Autowired
    BloodStorgeWarningDao bloodStorgeWarningDao;

    @Autowired
    BloodInformationDao bloodInformationDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        //创建一个接收对象
        BloodStorgeWarningEntity bloodStorgeWarningEntity = new BloodStorgeWarningEntity();
        if(Strings.isNotBlank((String) params.get("aboTypeCode"))){
            bloodStorgeWarningEntity.setAboTypeCode((String) params.get("aboTypeCode"));//设置abo类型
        }
        if(Strings.isNotBlank((String) params.get("bloodWarningValue"))){
            bloodStorgeWarningEntity.setBloodWarningValue(Integer.valueOf((String) params.get("bloodWarningValue")));
        }
        if(Strings.isNotBlank((String) params.get("timeWarningValue"))){
            bloodStorgeWarningEntity.setTimeWarningValue(new BigDecimal((String) params.get("timeWarningValue")));
        }
        // 时间格式化，解决Date->String 报错
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String queryTimeStart = (String)params.get("queryTimeStart");
        String queryTimeEnd = (String) params.get("queryTimeEnd");
        Date start = null;
        Date end = null;
        if(Strings.isNotBlank(queryTimeStart) && queryTimeStart!="null"){

            try {
                start = sdf.parse(queryTimeStart);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        if(Strings.isNotBlank(queryTimeEnd) && queryTimeEnd!="null"){
            try {
                end = sdf.parse(queryTimeEnd);
            }catch (Exception e){
                e.printStackTrace();
            }
        }

        log.info(bloodStorgeWarningEntity.toString());
        LambdaQueryWrapper<BloodStorgeWarningEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Strings.isNotBlank(bloodStorgeWarningEntity.getAboTypeCode()),BloodStorgeWarningEntity::getAboTypeCode,bloodStorgeWarningEntity.getAboTypeCode())
                .eq(Strings.isNotBlank(String.valueOf(bloodStorgeWarningEntity.getBloodWarningValue())) && bloodStorgeWarningEntity.getBloodWarningValue()!=null,BloodStorgeWarningEntity::getBloodWarningValue,bloodStorgeWarningEntity.getBloodWarningValue())
                .eq(Strings.isNotBlank(String.valueOf(bloodStorgeWarningEntity.getTimeWarningValue())) && bloodStorgeWarningEntity.getTimeWarningValue()!=null,BloodStorgeWarningEntity::getTimeWarningValue,bloodStorgeWarningEntity.getTimeWarningValue())
                .eq(BloodStorgeWarningEntity::getIfShow,IF_SHOW.code())
                .ge(start != null, BloodStorgeWarningEntity::getQueryTime, start)
                .le(end != null, BloodStorgeWarningEntity::getQueryTime, end);

        IPage<BloodStorgeWarningEntity> page = this.page(
                new Query<BloodStorgeWarningEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }


    @Override
    public PageUtils  storgeWarningByBagValue(Map<String, Object> params) {
        IPage<BloodStorgeWarningVo> page  = bloodStorgeWarningDao.warningSelect(new Query<BloodStorgeWarningVo>().getPage(params));

        return new PageUtils(page);
    }

    @Override
    public PageUtils storgeWarningByTime(Map<String, Object> params) {
        IPage<BloodStorgeWarningVo> page  = bloodStorgeWarningDao.warningSelectByTime(new Query<BloodStorgeWarningVo>().getPage(params));
        return new PageUtils(page);
    }

    @Override
    public PageUtils newStorgeWarningByTime(Map<String, Object> params) {
        IPage<BloodWarningVo> page = bloodStorgeWarningDao.newWarningSelectByTime(new Query<BloodWarningVo>().getPage(params));
        return new PageUtils(page);
    }

    @Override
    public List<BloodWarningVo> expired() {
        List<BloodInformationEntity> list = bloodInformationDao.selectList(
                new LambdaQueryWrapper<BloodInformationEntity>()
                        .eq(BloodInformationEntity::getBloodState, BloodInformationEnum.BLOOD_IN_STOCK.code())
        );
        List<BloodWarningVo> result = new ArrayList<>();
        Date now = new Date();
        for (BloodInformationEntity entity : list) {
            if (entity.getFailureTime() != null && now.after(entity.getFailureTime())) {
                BloodWarningVo vo = new BloodWarningVo();
                BeanUtils.copyProperties(entity, vo);
                // 计算过期天数（精确到时分秒）
                long diffMillis = now.getTime() - entity.getFailureTime().getTime();
                long expiredDays = diffMillis / (1000 * 60 * 60 * 24); // 只取整天数
                vo.setExpiredDays(expiredDays);
                result.add(vo);
            }
        }
        return result;
    }


}